#include<bits/stdc++.h>
#define sd(n) scanf("%d",&n) 
#define pd(n) printf("%d", (n))
#define x first
#define y second
using namespace std;
//----------------------------------------------------------------------------//
const int N = 110; 
typedef pair<int, int> PII;
int n, m,a,b,e,f;
int g[N][N];//存放地图
int d[N][N];//存每一个点到起点的距离
PII q[N * N];
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
int bfs()
{
    int hh = 0, tt = 0;
    q[0] = {a-1, b-1};

    memset(d, - 1, sizeof d);

    bool ok=false;
    d[a-1][b-1] = 0;
    while(hh <= tt)
    {
        PII t = q[hh ++ ];

        for(int i = 0; i < 4; i ++ )
        {
            int x = t.first + dx[i], y = t.second + dy[i];
            if(x >= 0 && x < n && y >= 0 && y < m && g[x][y] == 1 && d[x][y] == -1)
            {
                d[x][y] = d[t.first][t.second] + 1;
                q[ ++ tt ] = {x, y};
                if(x==e-1&&y==f-1) goto ed;
            }
        }
    }
    ed:;
    return d[e-1][f-1];
}
int main() 
{
    sd(n);sd(m);
    for(int i = 0; i < n; i ++ )
        for(int j = 0; j < m; j ++ )
            cin >> g[i][j];
    
    sd(a);sd(b);sd(e);sd(f);
    cout << bfs() << endl;
    return 0;
}

